package com.it.park.rms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.rms.client.model.User;
import com.it.park.rms.client.service.UserService;
import com.it.park.rms.common.MD5;
import com.it.park.rms.common.Page;
import com.it.park.rms.common.PageData;
import com.it.park.rms.common.Result;
import com.it.park.rms.constant.Constant;
import com.it.park.rms.constant.Global;
import com.it.park.rms.constant.UserStatus;

@Controller
@RequestMapping("/admin")
public class AdminUserController {
	private String rootPath=Constant.ADMIN_PATH;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("login")
	public String login(HttpServletRequest request){
		return rootPath+"login";
	}
	
	@RequestMapping("userList")
	public String userList(HttpServletRequest request){
			return rootPath+"user/userList";
	}
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<User> listRows(HttpServletRequest request){
		PageData<User> pageData = new PageData<>();
		int pageNow = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		User user = new User();
		Integer total = userService.selectTotal(user);
		Page page = new Page(pageNow,total,rows);
		user.setStartPos(page.getStartPos());
		user.setPageSize(page.getPageSize());
		List<User> list = userService.selectPagination(user);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	/**
	 * 删除用户
	 */
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<Boolean> deleteById(Integer id,HttpServletRequest request){
		Result<Boolean> result = new Result<>(true);
		boolean success = userService.deleteByPrimaryKey(id);
		if(success){
			result.setMessage("删除成功!");
		}else{
			result.setSuccess(false);
			result.setMessage("删除失败!");
		}
		return result;
	}
	
	/**
	 * 添加用户
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result<Boolean> save(User user,HttpServletRequest request){
			Result<Boolean> result = new Result<>(true);
			String md5Str = MD5.md5(user.getPassword()+user.getUserName());
			user.setPassword(MD5.md5(md5Str));
			boolean success = userService.insertSelective(user);
			if(success){
				result.setMessage(Constant.SAVE_SUCCESS_MSG);
			}else{
				result.setSuccess(false);
				result.setMessage(Constant.SAVE_FAIL_MSG);
			}
			return result;
	}
	
	/**
	 * 修改用户
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Result<Boolean> edit(User user,HttpServletRequest request){
		Result<Boolean> result = new Result<>(true);
		boolean success = userService.updateByPrimaryKeySelective(user);
		if(success){
			result.setMessage(Constant.UPDATE_SUCCESS_MSG);
		}else{
			result.setSuccess(false);
			result.setMessage(Constant.UPDATE_FAIL_MSG);
		}
		return result;
	}
	
	/**
	 *冻结帐户
	 */
	@RequestMapping("freezeAccount")
	@ResponseBody
	public Result<Boolean> freezeAccount(@RequestParam("ids[]")Integer[] ids){
		Result<Boolean> result = new Result<>(true);
		if(ids.length <=0){
			return result;
		}
		User user = null;
		List<User> list = new ArrayList<>();
		for (int i = 0; i < ids.length; i++) {
			user = new User();
			user.setId(ids[i]);
			list.add(user);
		}
		boolean success = userService.batchUpdate(list);
		if(success){
			result.setMessage(Constant.FREEZE_ACCOUNT_SUCCESS_MSG);
		}else{
			result.setSuccess(false);
			result.setMessage(Constant.FREEZE_ACCOUNT_FAIL_MSG);
		}
		return result;
	}
	
	/**
	 * 登录验证
	 **/
	@RequestMapping("loginCheck")
	@ResponseBody//将对象转化为json对象
	public Result<User> loginCheck(User user ,HttpServletRequest request,HttpSession session){
			Result<User> result = new Result<>(true);
			User u= userService.login(user.getUserName());
			String password = MD5.md5(user.getPassword());
			boolean success = false ;
			if(u == null || !u.getPassword().equals(password)){
				result.setMessage(Constant.USER_NAME_OR_PWD_ERROR);
				result.setSuccess(success);
			}else{
				if(Constant.USER_TYPE == u.getUserType()){//非管理员不允许进后台
					if(UserStatus.ENABLED.code == u.getStatus()){
       			 		session.setAttribute(Global.USER_SESSION, u);
	       			 	result.setMessage(Constant.LOGIN_SUCCESS);
						result.setObj(u);
						return result;
					}else if(UserStatus.UNAUTHORIZED.code == u.getStatus()){
						result.setMessage(UserStatus.UNAUTHORIZED.desc);
					}else{
						result.setMessage(UserStatus.DISABLED.desc);
					}
					result.setSuccess(success);
				}else{
					result.setMessage(Constant.LOGIN_FAIL);
					result.setSuccess(success);
				}
			}
			return result;
	}
	
	/**
	 * 退出
	 */
	@RequestMapping("logout")
	public String logout(HttpSession httpSession,HttpServletRequest request){
		User user = (User) httpSession.getAttribute(Global.USER_SESSION);
		if(user != null){
			httpSession.invalidate();
		}
		return  "redirect:login";
	}
	
	
	@RequestMapping("index")
	public String index(User user,HttpServletRequest request){
		return rootPath+"index";
	}
	
	@RequestMapping("center")
	public String center(HttpServletRequest request){
		return rootPath+"center";
	}
	
	/**
	 * 用户分配角色
	 */
	@RequestMapping("assignRole")
	@ResponseBody
	public Result<String> assignRole(User user,HttpServletRequest request){
		Result<String> result = new Result<>(true);
		return result;
	}
	
	/**
	 * 重置密码
	 */
	@RequestMapping("resetPwd")
	@ResponseBody
	public Result<Boolean> resetPwd(User user){
		Result<Boolean> result = new Result<>(true);
		boolean success = userService.updatePwd(user);
		if(!success){
			result.setSuccess(false);
			result.setMessage(Constant.RESET_PWD_FAIL);
		}
		return result;
	}
	
}
